package ysq.xyg.sys.controller;

import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.util.BaseConstant;

import cn.hutool.core.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.sys.model.po.SysMenu;
import ysq.xyg.sys.model.vo.SysMenuVo;
import ysq.xyg.sys.service.SysMenuService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 系统管理-菜单controller层
 *
 * @author shengQiang yu
 * @date 2019-4-1 10:31:20
 */
@RestController
public class SysMenuController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(SysMenuController.class);
    @Resource
    private SysMenuService sysMenuService;

    private SysMenu sysMenu;

    /**
     * 分页查询
     *
     * @param sysMenuVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @BaseLog(module = "系统管理",className = "菜单",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysMenu/page",method = RequestMethod.POST)
    public ReturnEntity page(SysMenuVo sysMenuVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = sysMenuService.page(sysMenuVo,pageSize,pageNum);
        return returnEntity;
    }
    /**
     * 批量查询
     *
     * @param sysMenuVo 对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @BaseLog(module = "系统管理",className = "菜单",methods = "批量查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysMenu/list",method = RequestMethod.POST)
    public ReturnEntity list(SysMenuVo sysMenuVo){
        sysMenuVo.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
        ReturnEntity returnEntity = new ReturnEntity(sysMenuService.acceptList(sysMenuVo));
        return returnEntity;
    }
    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @RequestMapping(value = "/sysMenu/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(String id,String parentId){
        ModelAndView view = new ModelAndView("/sys/sysMenuUpdateOrSave");
        SysMenu sysMenu=new SysMenu();
        if(StrUtil.isNotEmpty(id)){
            sysMenu = sysMenuService.selectByPrimaryKey(id);
        }
        if(StrUtil.isNotEmpty(parentId)){
            SysMenu parentSysMenu = sysMenuService.selectByPrimaryKey(parentId);
            if(parentSysMenu!=null){
                sysMenu.setParentMenuName(parentSysMenu.getMenuName());
                sysMenu.setParentId(parentId);
            }
        }
        view.addObject("sysMenu", sysMenu);
        return view;
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @BaseLog(module = "系统管理",className = "菜单",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysMenu/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return sysMenuService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 添加或修改
     *
     * @param sysMenuVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @BaseLog(module = "系统管理",className = "菜单",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysMenu/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,SysMenuVo sysMenuVo){
        return sysMenuService.addOrUpdate(sysMenuVo,acceptCurrentUser(request));
    }
    /**
     * 获取菜单树
     *
     * @param sysMenuVo Vo对象
     * @author shengQiang yu
     * @date 2019-4-1 10:31:20
     */
    @BaseLog(module = "系统管理",className = "菜单",methods = "获取菜单树",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/sysMenu/dTreeList",method = RequestMethod.POST)
    public ReturnEntity dTreeList(HttpServletRequest request,SysMenuVo sysMenuVo){
        return sysMenuService.dTreeList(sysMenuVo);
    }
}